﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel;
using System.IO;
using System.Xml;
using System.ServiceModel.Syndication;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Linq;

namespace PicasaManager
{
    public class GoogleAlbum:INotifyPropertyChanged
    {
        public GoogleAlbum()
        {
            Media = new GoogleMedia();
            Photos = new List<GooglePhoto>();
        }

        public string Id { get; set; }
        public DateTime Published { get; set; }
        public DateTime Updated { get; set; }
        public string Title { get; set; }
        public string Summary { get; set; }
        public string Author { get; set; }
        public string AuthorUri { get; set; }
        public string AuthorNickname { get; set; }
        public int PhotoCount { get; set; }
        public bool CommentsEnabled { get; set; }
        public int CommentCount { get; set; }
        public int TimeStamp { get; set; }

        public GoogleMedia Media { get; set; }

        public List<GooglePhoto> Photos { get; set; }
       
        public event PropertyChangedEventHandler PropertyChanged;

        public static GoogleAlbum FromXmlStream(Stream stream)
        {
            GoogleAlbum album = new GoogleAlbum();

            XmlReader responseReader = XmlReader.Create(stream);
            SyndicationFeed feed = SyndicationFeed.Load(responseReader);

            foreach (var item in feed.Items)
            {
                GooglePhoto photo = new GooglePhoto();

                XElement mediaGroup = item.ElementExtensions.Where(ex => ex.OuterNamespace == "http://search.yahoo.com/mrss/").FirstOrDefault().GetObject<XElement>();
                XElement exif = item.ElementExtensions.Where(ex => ex.OuterNamespace == "http://schemas.google.com/photos/exif/2007").FirstOrDefault().GetObject<XElement>();
                var georss = item.ElementExtensions.Where(ex => ex.OuterNamespace == "http://www.georss.org/georss").FirstOrDefault();


                foreach (var descendant in mediaGroup.Descendants())
                {
                    switch (descendant.Name.LocalName)
                    {
                        case "content": photo.Media.Contents.Add(GoogleMediaContent.FromXElement(descendant)); break;
                        case "credit": photo.Media.Credit = descendant.Value; break;
                        case "description": photo.Media.Description = descendant.Value; break;
                        case "keywords": photo.Media.Description = descendant.Value; break;
                        case "thumbnail": photo.Media.Thumbnails.Add(GoogleMediaThumbnail.FromXElement(descendant)); break;
                        case "title": photo.Media.Title = descendant.Value; break;
                    }
                }

                foreach (var descendant in exif.Descendants())
                {
                    switch (descendant.Name.LocalName)
                    {
                        case "fstop":  break;
                        case "make": break;
                        case "description":  break;
                        case "exposure": break;
                        case "flash": break;
                        case "focallength": break;
                        case "iso":  break;
                        case "time": break;
                        case "model": break;
                    }
                }

                if (georss != null)
                {
                    var pos = georss.GetObject<XElement>().Value;
                }

                foreach (var ex in item.ElementExtensions.Where(ex => ex.OuterNamespace == "http://schemas.google.com/photos/2007"))
                {
                    var element = ex.GetObject<XElement>();
                    switch (ex.OuterName)
                    {
                        case "id": photo.Id = element.Value; break;
                        case "albumid": photo.AlbumId = element.Value; break;
                        case "name": break;
                        case "location": break;
                        case "access": break;
                        case "timestamp": break;
                        case "numphotos":  break;
                        case "user": break;
                        case "nickname": break;
                        case "commentingEnabled":  break;
                        case "commentCount":  break;
                    }
                }

                       album.Photos.Add(photo);

                //gml: http://www.opengis.net/gml
                //georss: http://www.georss.org/georss
                //exif: http://schemas.google.com/photos/exif/2007
            }

            return album;
        }
    }
}
